package com.tju.ocean_ecology_website.service.impl;

import com.tju.ocean_ecology_website.entity.TeamMember;
import com.tju.ocean_ecology_website.dao.TeamMemberDao;
import com.tju.ocean_ecology_website.service.TeamMemberService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.Calendar;

/**
 * 团队成员表(TeamMember)表服务实现类
 *
 * @author shaolong
 * @since 2025-04-29 19:34:20
 */
@Service("teamMemberService")
public class TeamMemberServiceImpl implements TeamMemberService {
    @Resource
    private TeamMemberDao teamMemberDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TeamMember queryById(Long id) {
        return this.teamMemberDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param teamMember 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<TeamMember> queryByPage(TeamMember teamMember, PageRequest pageRequest) {
        long total = this.teamMemberDao.count(teamMember);
        return new PageImpl<>(this.teamMemberDao.queryAllByLimit(teamMember, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param teamMember 实例对象
     * @return 实例对象
     */
    @Override
    public TeamMember insert(TeamMember teamMember) {
        this.teamMemberDao.insert(teamMember);
        return teamMember;
    }

    /**
     * 修改数据
     *
     * @param teamMember 实例对象
     * @return 实例对象
     */
    @Override
    public TeamMember update(TeamMember teamMember) {
        this.teamMemberDao.update(teamMember);
        return this.queryById(teamMember.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.teamMemberDao.deleteById(id) > 0;
    }

    /**
     * 审核通过成员
     *
     * @param id 成员ID
     * @return 实例对象
     */
    @Override
    public TeamMember approveMember(Long id) {
        TeamMember member = this.queryById(id);
        if (member != null) {
            member.setAuditStatus(1); // 已通过
            member.setRejectReason(null); // 清空拒绝原因
            this.teamMemberDao.update(member);
            return this.queryById(id);
        }
        return null;
    }

    /**
     * 拒绝成员申请
     *
     * @param id 成员ID
     * @param reason 拒绝原因
     * @return 实例对象
     */
    @Override
    public TeamMember rejectMember(Long id, String reason) {
        TeamMember member = this.queryById(id);
        if (member != null) {
            member.setAuditStatus(2); // 已拒绝
            member.setRejectReason(reason); // 设置拒绝原因
            this.teamMemberDao.update(member);
            return this.queryById(id);
        }
        return null;
    }

    /**
     * 发送邀请
     *
     * @param teamId 团队ID
     * @param email 邮箱
     * @param role 角色
     * @param message 邀请消息
     * @return 邀请码
     */
    @Override
    public String sendInvitation(Long teamId, String email, Integer role, String message) {
        // 生成邀请码
        String inviteCode = UUID.randomUUID().toString().replace("-", "");

        // 设置过期时间，默讧七天后过期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        Date expireTime = calendar.getTime();

        // 创建成员记录，状态为待审核
        TeamMember member = new TeamMember();
        member.setTeamId(teamId);
        member.setRole(role);
        member.setStatus(0); // 活跃
        member.setAuditStatus(0); // 待审核
        member.setInviteCode(inviteCode);
        member.setInviteExpireTime(expireTime);
        member.setJoinTime(new Date());

        // 保存成员记录
        this.teamMemberDao.insert(member);

        // TODO: 发送邮件邀请

        return inviteCode;
    }

    /**
     * 获取邀请链接
     *
     * @param teamId 团队ID
     * @return 邀请链接
     */
    @Override
    public String getInviteLink(Long teamId) {
        // 生成通用邀请码
        String inviteCode = UUID.randomUUID().toString().replace("-", "");

        // 设置过期时间，默讧七天后过期
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        Date expireTime = calendar.getTime();

        // 创建成员记录，状态为待审核
        TeamMember member = new TeamMember();
        member.setTeamId(teamId);
        member.setRole(2); // 普通成员
        member.setStatus(0); // 活跃
        member.setAuditStatus(0); // 待审核
        member.setInviteCode(inviteCode);
        member.setInviteExpireTime(expireTime);

        // 保存成员记录
        this.teamMemberDao.insert(member);

        // 返回邀请链接
        return "http://localhost:8080/join?code=" + inviteCode;
    }

    /**
     * 导出成员信息
     *
     * @param teamMember 查询条件
     * @return 成员列表
     */
    @Override
    public List<TeamMember> exportMembers(TeamMember teamMember) {
        return this.teamMemberDao.exportMembers(teamMember);
    }

    /**
     * 通过邀请码查询成员
     *
     * @param inviteCode 邀请码
     * @return 成员对象
     */
    @Override
    public TeamMember queryByInviteCode(String inviteCode) {
        return this.teamMemberDao.queryByInviteCode(inviteCode);
    }

    /**
     * 根据团队和用户查询成员
     *
     * @param teamId 团队ID
     * @param userId 用户ID
     * @return 成员对象
     */
    @Override
    public TeamMember queryByTeamAndUser(Long teamId, Long userId) {
        return this.teamMemberDao.queryByTeamAndUser(teamId, userId);
    }

    /**
     * 统计符合条件的数据数量
     *
     * @param teamMember 查询条件
     * @return 数据数量
     */
    @Override
    public long count(TeamMember teamMember) {
        return this.teamMemberDao.count(teamMember);
    }
}
